%{

#include "main.h"
#include "Parse.tab.h"
extern "C" {
    extern int yydebug;
    int commentStartLineNo;
    int yylex(void);
    int yywrap(void);
}
%}

%x BLOCK_COMMENT
%x LINE_COMMENT

%%

CREATE              { return CREATE; }
DROP                { return DROP; }
DATABASE            { return DATABASE; }
TABLE               { return  TABLE; }
SHOW                { return SHOW; }
TABLES              { return  TABLES; }
USE                 { return USE; }
INSERT              { return INSERT; }
INTO                { return INTO; }
SELECT              { return SELECT; }
FROM                { return FROM; }
WHERE               { return WHERE; }
LIKE                { return LIKE; }
UPDATE              { return UPDATE; }
SET                 { return SET; }
IS                  { return IS; }
PRIMARY             { return PRIMARY; }
FOREIGN             { return FOREIGN; }
KEY                 { return KEY; }
NOT                 { return NOT; }
NULL                { return TOKEN_NULL; }
AND                 { return AND; }
OR                  { return OR; }
"!="                { return NOT_EQUAL; }
"<>"                { return NOT_EQUAL; }
">="                { return GREATER_EQUAL; }
"<="                { return LESS_EQUAL; }
DELETE              { return DELETE; }
INT                 { return INTNUM; }
DOUBLE              { return DOUBLE; }
varchar             { return VARCHAR; }
decimal             { return DECIMAL; }
char                { return CHAR; }
JOIN                { return JOIN; }
OUTER               { return OUTER; }
LEFT                { return LEFT; }
RIGHT               { return RIGHT; }
INNER               { return INNER; }
UNION               { return UNION; }
IN                  { return IN; }
VALUES              { return VALUES; }
DESC                { return DESC; }
ON                  { return ON; }
SUM                 { return SUM; }
MIN                 { return MIN; }
MAX                 { return MAX; }
COUNT               { return COUNT; }
AVG                 { return AVG; }
GROUP               { return GROUP; }
BY                  { return BY; }
\/\*                { BEGIN(BLOCK_COMMENT);  commentStartLineNo = yylineno; }
<BLOCK_COMMENT>\*\/ {BEGIN(INITIAL); }
<BLOCK_COMMENT><<EOF>>  { fprintf(stderr, "Warning: unclosed comment beginning on line %d\n",
                                  ++commentStartLineNo); return EOF; }
<BLOCK_COMMENT>\n       { yylineno++; }
<BLOCK_COMMENT>.        { /* ignore */ }

"--"                    { BEGIN(LINE_COMMENT); }
<LINE_COMMENT>\n        { BEGIN(INITIAL); yylineno++; }
<LINE_COMMENT>.         { /* ignore */ }

[a-zA-Z][a-zA-Z0-9_]*   { yylval.stringValue = std::string(strdup(yytext));
                          if (yydebug) printf("lexed identifier '%s'\n", yytext);
                          return IDENTIFIER; }
((\"[^\"]*\")|(\'[^\']*\')) { yylval.stringValue = std::string(strndup(yytext+1, strlen(yytext) - 2)); return STRING_LITERAL; }
[0-9]+                 { yylval.intValue = atoi(yytext); return INTEGER; }
([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) { yylval.doubleValue = atof(yytext); return DOUBLE_LITERAL; }
[ \t\r]+                  { /* ignore */ }
\n                      { yylineno++; }
.                       { if (yydebug) printf("lexed single character '%c'\n", yytext[0]); 
                          return yytext[0]; }

%%
/*int yywrap() {
   // open next reference or source file and start scanning
   if((yyin = getNextFile()) != NULL) {
      yylineno = 0; // reset line counter for next source file
      return 0;
   }
   return 1;
}*/
